package com.hspedu.homework.homework01;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/23
 * @time 19:03
 */
public class PrivateTest {
    private String name = "hello kitty";

    public String getName() {
        return name;
    }

}

class Test{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<?> aClass = Class.forName("com.hspedu.homework.homework01.PrivateTest");
        Field name = aClass.getDeclaredField("name");
        Object o = aClass.newInstance();
        name.setAccessible(true);
        name.set(o,"hello hsp");
        Method getName = aClass.getMethod("getName");
        System.out.println(getName.invoke(o));
    }
}


